﻿// -------------------------------------------------------------------------
//    @FileName         :    NFCItyPacketSendUtil.cpp
//    @Author           :    Gao.Yi
//    @Date             :    2025-09-16
//    @Email			:    445267987@qq.com
//    @Module           :    NFCItyPacketSendUtil
//    @Desc             :
//
// -------------------------------------------------------------------------

#include "NFCityPacketSendUtil.h"

#include "NFCityPlayer.h"
#include "NFCitySession.h"
#include "proto_access2zone.nanopb.h"
#include "NFLogicCommon/NFScopedMessageHead.h"
#include "NFLogicCommon/NFWatchTime.h"
#include "NFServerComm/NFServerCommon/NFIServerMessageModule.h"

int NFCityPacketSendUtil::SendToClient(NFCitySession* pSession, uint32_t cmd, const google::protobuf::Message* pstMsgRes)
{
    CHECK_NULL(0, pSession);
    CHECK_NULL(0, pstMsgRes);
    CHECK_EXPR(pstMsgRes->IsInitialized(), -2, "{}", pstMsgRes->InitializationErrorString());

    LOGSVR_TRACE("ConnIndex:" << pSession->GetConnIndex() << " ObjID:" << pSession->GetGlobalId() << " MsgType:" << cmd);

    SendToClientDirectly(pSession, cmd, pstMsgRes);
    return 0;
}

int NFCityPacketSendUtil::SendToClient(NFCityPlayer* pPlayer, uint32_t cmd, const google::protobuf::Message* pstMsgRes)
{
    CHECK_NULL(0, pPlayer);

    NFCitySession* pSession = pPlayer->GetClientSession();
    CHECK_PLAYER_EXPR(pPlayer->GetRoleID(), pSession, -1, "");

    return SendToClient(pSession, cmd, pstMsgRes);
}

int NFCityPacketSendUtil::SendToClientDirectly(NFCitySession* pSession, uint32_t cmd, const google::protobuf::Message* pstMsgRes)
{
    CHECK_EXPR(pSession, -1, "");
    CHECK_EXPR(pstMsgRes, -1, "");
    CHECK_EXPR(pstMsgRes->IsInitialized(), -2, "{}", pstMsgRes->InitializationErrorString());

    LOGSVR_TRACE("ConnIndex:" << pSession->GetConnIndex() << " ObjID:" << pSession->GetGlobalId() << " MsgType:" << cmd);

    int iRetCode = 0;

    if (pSession->GetConnIndex() < 0)
    {
        iRetCode = pSession->GetConnIndex();
        LOGSVR_ERROR("Invalid ConnIndex:" << pSession->GetConnIndex() << " ObjID:" << pSession->GetGlobalId() << " MsgType:" << cmd << " ResRetCode:" << iRetCode);
        return iRetCode;
    }

    /*if (pstMsgRes->GetCachedSize() != pstMsgRes->ByteSize())
    {
        LOGSVR_ERROR("pstMsgRes->GetCachedSize()= " << pstMsgRes->GetCachedSize() << " not equal pstMsgRes->ByteSize()=" << pstMsgRes->ByteSize());
    }*/

#ifdef NF_DEBUG_MODE
    NFWatchTime oWatchTime(cmd, 0, pstMsgRes->GetCachedSize(), ENST_NET_OUT, &iRetCode);
#endif

    NFrame::Proto_FramePkg pkg;
    pkg.set_msg_id(cmd);
    pkg.set_module_id(NF_MODULE_CLIENT);
    pkg.set_msg_data(pstMsgRes->SerializePartialAsString());
    int curSeq = pSession->IncreaseServerSeq();
    pkg.mutable_disp_info()->set_seq(curSeq);
    pkg.mutable_disp_info()->set_req_seq(NFScopedMessageHead::GetStackRequestSeq(pSession->GetGlobalId(), cmd));

    std::string pkgData = pkg.SerializePartialAsString();
    FindModule<NFIServerMessageModule>()->SendMsgToProxyServer(NF_ST_CITY_SERVER, pSession->GetConnBusId(), E_ACCESS2ZONE_TRANSFER, pkgData.data(), pkgData.size(), pSession->GetConnIndex(), pSession->GetGlobalId());

    LOGSVR_TRACE_IF(cmd != proto_ff::E_CS_HEARTBEAT_RSP, pstMsgRes->GetTypeName() << "[seq:" << curSeq << " cmd:" << cmd << " " << proto_ff::Proto_CS_CMD_Name(static_cast<proto_ff::Proto_CS_CMD>(cmd)) << " len:" << pkg.msg_data().size() << "]\n");

    LOGSVR_TRACE_IF(cmd != proto_ff::E_CS_HEARTBEAT_RSP, pkg.DebugString() << "\n" << pstMsgRes->DebugString());

    return 0;
}

int NFCityPacketSendUtil::SendToClientFinally(NFCitySession* pSession, void* pBuff, uint32_t ulSize)
{
    CHECK_NULL(0, pSession);
    CHECK_NULL(0, pBuff);

    LOGSVR_DEBUG("ConnIndex:" << pSession->GetConnIndex() << " ObjID:" << pSession->GetGlobalId() << ", size=" << ulSize);

    int iRetCode = 0;
    if (pSession->GetConnIndex() < 0)
    {
        iRetCode = pSession->GetConnIndex();
        LOGSVR_ERROR("Invalid ConnIndex:" << pSession->GetConnIndex() << " ObjID:" << pSession->GetGlobalId() << " ResRetCode:" << iRetCode);
        return iRetCode;
    }

    FindModule<NFIServerMessageModule>()->SendMsgToProxyServer(NF_ST_CITY_SERVER, pSession->GetConnBusId(), E_ACCESS2ZONE_TRANSFER, (const char*)pBuff, ulSize, pSession->GetConnIndex(), pSession->GetGlobalId());
    return 0;
}

int NFCityPacketSendUtil::SendErrToClient(NFCitySession* pSession, uint32_t req_cmd, proto_ff::Proto_CS_ErrorCode result)
{
    proto_ff::Proto_CSRsp message;
    proto_ff::Proto_CSErrorRsp* pErrorRsp = message.mutable_error();
    pErrorRsp->set_req_msg_id(req_cmd);
    pErrorRsp->set_error(result);

    return SendToClient(pSession, proto_ff::E_CS_ERROR, &message);
}

int NFCityPacketSendUtil::SendErrToClient(NFCityPlayer* pPlayer, uint32_t req_cmd, proto_ff::Proto_CS_ErrorCode result)
{
    CHECK_NULL(0, pPlayer);

    NFCitySession* pSession = pPlayer->GetClientSession();
    CHECK_PLAYER_EXPR(pPlayer->GetRoleID(), pSession, -1, "");

    return SendErrToClient(pSession, req_cmd, result);
}

int NFCityPacketSendUtil::SendMsgToLogicServer(NFCityPlayer* pPlayer, uint32_t cmd, const google::protobuf::Message* pstMsg)
{
    CHECK_NULL(0, pPlayer);
    CHECK_NULL(0, pstMsg);
    FindModule<NFIServerMessageModule>()->SendMsgToLogicServer(NF_ST_CITY_SERVER, pPlayer->GetLogicSvrId(), cmd, *pstMsg, pPlayer->GetRoleID());
    return 0;
}

int NFCityPacketSendUtil::SendTransToLogicServer(NFCityPlayer* pPlayer, uint32_t cmd, const google::protobuf::Message* pstMsg, uint32_t req_trans_id, uint32_t rsp_trans_id)
{
    CHECK_NULL(0, pPlayer);
    CHECK_NULL(0, pstMsg);
    FindModule<NFIServerMessageModule>()->SendTransToLogicServer(NF_ST_CITY_SERVER, pPlayer->GetLogicSvrId(), cmd, *pstMsg, pPlayer->GetRoleID(), req_trans_id, rsp_trans_id);
    return 0;
}
